package com.yiqi.zhiyuan.feature.order.view;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.AMap;
import com.amap.api.maps.LocationSource;
import com.amap.api.maps.MapView;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MyLocationStyle;
import com.amap.api.maps.model.Poi;
import com.amap.api.navi.AmapNaviPage;
import com.amap.api.navi.AmapNaviParams;
import com.amap.api.navi.AmapNaviType;
import com.amap.api.services.core.AMapException;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.route.BusRouteResult;
import com.amap.api.services.route.DrivePath;
import com.amap.api.services.route.DriveRouteResult;
import com.amap.api.services.route.RideRouteResult;
import com.amap.api.services.route.RouteSearch;
import com.amap.api.services.route.WalkRouteResult;
import com.github.nukc.stateview.StateView;
import com.luck.picture.lib.entity.EventEntity;
import com.luck.picture.lib.permissions.RxPermissions;
import com.luck.picture.lib.rxbus2.RxBus;
import com.luck.picture.lib.rxbus2.Subscribe;
import com.luck.picture.lib.rxbus2.ThreadMode;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.DateUtil;
import com.suozhang.framework.utils.LocationUtils;
import com.suozhang.framework.utils.T;
import com.suozhang.framework.utils.logger.Logger;
import com.suozhang.framework.widget.dialog.NormalDialog;
import com.suozhang.framework.widget.dialog.OnBtnClickL;
import com.yiqi.zhiyuan.R;
import com.yiqi.zhiyuan.common.map.AMapUtil;
import com.yiqi.zhiyuan.common.map.DriveRouteOverlay;
import com.yiqi.zhiyuan.entity.vo.EndPushVO;
import com.yiqi.zhiyuan.entity.vo.OrderVO;
import com.yiqi.zhiyuan.feature.order.contract.OrderInfoContract;
import com.yiqi.zhiyuan.feature.order.dependencies.info.DaggerOrderInfoComponent;
import com.yiqi.zhiyuan.feature.order.dependencies.info.OrderInfoPresenterModule;
import com.yiqi.zhiyuan.feature.order.view.adapter.CancleOrderDialog;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;
import io.rong.imkit.RongIM;

import static com.yiqi.zhiyuan.framework.ZYConstants.ORDER_CANCLE;

public class OrderInfoActivity extends BaseActivity implements LocationSource, AMapLocationListener, Toolbar.OnMenuItemClickListener, OrderInfoContract.View,
        AMap.OnMapClickListener, AMap.OnMarkerClickListener, AMap.OnInfoWindowClickListener, AMap.InfoWindowAdapter, RouteSearch.OnRouteSearchListener,
        CancleOrderDialog.MyClickListenerInterface {


    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.map_order)
    MapView mMapOrder;
    AMap aMap;
    //声明AMapLocationClient类对象
    public AMapLocationClient mLocationClient = null;

    //声明AMapLocationClientOption对象
    public AMapLocationClientOption mLocationOption = null;

    @BindView(R.id.tv_order_start_address)
    TextView mTvOrderStartAddress;
    @BindView(R.id.tv_order_end_address)
    TextView mTvOrderEndAddress;
    @BindView(R.id.tv_order_time)
    TextView mTvOrderTime;
    @BindView(R.id.tv_order_remark)
    TextView mTvOrderRemark;
    @BindView(R.id.btn_order_look_up)
    ImageView mBtnOrderLookUp;

    @Inject
    OrderInfoContract.Presenter mPresenter;
    OrderVO mOrderVO;
    @BindView(R.id.iv_order_avatar)
    ImageView mIvOrderAvatar;
    @BindView(R.id.tv_order_username)
    TextView mTvOrderUsername;
    @BindView(R.id.iv_order_gender)
    ImageView mIvOrderGender;
    @BindView(R.id.btn_order_passengers)
    TextView mTvOrder;
    @BindView(R.id.btn_order_service)
    TextView mBtnOrderService;
    @BindView(R.id.tv_order_point_1)
    TextView mTvOrderPoint1;
    @BindView(R.id.tv_order_point_2)
    TextView mTvOrderPoint2;
    @BindView(R.id.tv_order_arrived_time)
    TextView mTvOrderArrivedTime;
    @BindView(R.id.tv_order_label)
    TextView mTvOrderLabel;
    @BindView(R.id.stateView)
    StateView mStateView;

    private RouteSearch mRouteSearch;
    private DriveRouteResult mDriveRouteResult;
    private LatLonPoint mStartPoint = null;//起点
    private LatLonPoint mEndPoint = null;//终点
    private final int ROUTE_TYPE_DRIVE = 2;
    private NormalDialog mDialog;
    RxPermissions rxPermissions;
    AMapLocation mAMapLocation;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_order_info;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mMapOrder.onCreate(savedInstanceState);
    }

    @Override
    protected void initInjector() {
        DaggerOrderInfoComponent.builder().orderInfoPresenterModule(new OrderInfoPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        RxBus.getDefault().register(this);
        rxPermissions = new RxPermissions(this);
        initToolBar(mToolbar, "", true, true);
        mStateView.setRetryResource(R.layout.view_error);

        initMapUI();
        initMapLocation();
    }

    @Override
    protected void initData() {
        mStateView.showLoading();
        mOrderVO = (OrderVO) this.getIntent().getSerializableExtra("order_info");
        mPresenter.selectDriverOrderDetails(mOrderVO.getOrderNo(), mOrderVO.getUseCarType());
    }

    @Override
    protected void initEvent() {
        mToolbar.setOnMenuItemClickListener(this);
        if (!LocationUtils.isGpsEnabled()) {
            initGpsDialog();
        }

        mStateView.setOnRetryClickListener(new StateView.OnRetryClickListener() {
            @Override
            public void onRetryClick() {
                mPresenter.selectDriverOrderDetails(mOrderVO.getOrderNo(), mOrderVO.getUseCarType());
            }
        });
    }

    /**
     * 初始化地图UI
     */
    private void initMapUI() {
        aMap = mMapOrder.getMap();

        UiSettings mUiSettings = aMap.getUiSettings();//实例化UiSettings类对象
        mUiSettings.setZoomControlsEnabled(false);
        registerListener();
        mRouteSearch = new RouteSearch(this);
        mRouteSearch.setRouteSearchListener(this);
    }

    /**
     * 注册监听
     */
    private void registerListener() {
        aMap.setOnMapClickListener(this);
        aMap.setOnMarkerClickListener(this);
        aMap.setOnInfoWindowClickListener(this);
        aMap.setInfoWindowAdapter(this);

    }

    /****
     * 起点终点图标设置
     */
    private void setfromandtoMarker(OrderVO data) {

        mStartPoint = new LatLonPoint(data.getDepLatitude(), data.getDepLongitude());
        mEndPoint = new LatLonPoint(data.getDesLatitude(), data.getDesLongitude());
        searchRouteResult(ROUTE_TYPE_DRIVE, RouteSearch.DrivingDefault, data);
    }

    /**
     * 初始化定位
     */
    private void initMapLocation() {
        MyLocationStyle myLocationStyle = new MyLocationStyle();//初始化定位蓝点样式类myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//连续定位、且将视角移动到地图中心点，定位点依照设备方向旋转，并且会跟随设备移动。（1秒1次定位）如果不设置myLocationType，默认也会执行此种模式。
        myLocationStyle.interval(2000); //设置连续定位模式下的定位间隔，只在连续定位模式下生效，单次定位模式下不会生效。单位为毫秒。
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_FOLLOW_NO_CENTER);//连续定位、蓝点不会移动到地图中心点，定位点依照设备方向旋转，并且蓝点会跟随设备移动。
        myLocationStyle.radiusFillColor(Color.parseColor("#00000000"));
        myLocationStyle.strokeColor(Color.parseColor("#00000000"));
        aMap.setMyLocationStyle(myLocationStyle);//设置定位蓝点的Style
        aMap.setMyLocationEnabled(true);// 设置为true表示启动显示定位蓝点，false表示隐藏定位蓝点并不进行定位，默认是false。

        // 设置定位监听
        aMap.setLocationSource(this);
        // 设置为true表示显示定位层并可触发定位，false表示隐藏定位层并不可触发定位，默认是false
        aMap.setMyLocationEnabled(true);
        // 设置定位的类型为定位模式，有定位、跟随或地图根据面向方向旋转几种
        aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);

    }

    /**
     * 开始搜索路线规划方案
     */
    public void searchRouteResult(int routeType, int mode, OrderVO data) {
        if (mStartPoint == null) {
            showMsg("起点未设置");
            return;
        }
        if (mEndPoint == null) {
            showMsg("终点未设置");

        }

        List<LatLonPoint> wayPoints = new ArrayList<>();

        if (data.getWp1Latitude() > 0 && data.getWp1Longitude() > 0) {
            wayPoints.add(new LatLonPoint(data.getWp1Latitude(), data.getWp1Longitude()));
        }

        if (data.getWp2Latitude() > 0 && data.getWp2Longitude() > 0) {
            wayPoints.add(new LatLonPoint(data.getWp2Latitude(), data.getWp2Longitude()));
        }
        final RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(
                mStartPoint, mEndPoint);
        if (routeType == ROUTE_TYPE_DRIVE) {
            /***
             * fromAndTo，路径的起点终点；
             * mode，路径规划的策略，可选，默认为0-速度优先；详细策略请见驾车策略说明；
             * passedByPoints，途经点，可选；
             * avoidpolygons，避让区域，可选，支持32个避让区域，每个区域最多可有16个顶点。如果是四边形则有4个坐标点，如果是五边形则有5个坐标点。
             * avoidRoad，避让道路，只支持一条避让道路，避让区域和避让道路同时设置，只有避让道路生效。
             * */
            RouteSearch.DriveRouteQuery query = new RouteSearch.DriveRouteQuery(fromAndTo, mode, wayPoints, null, "");
            mRouteSearch.calculateDriveRouteAsyn(query);
        }
    }

    OnLocationChangedListener mListener;

    @Override
    public void activate(OnLocationChangedListener onLocationChangedListener) {
        mListener = onLocationChangedListener;
        if (mLocationClient == null) {
            //初始化定位
            mLocationClient = new AMapLocationClient(this);
            //初始化定位参数
            mLocationOption = new AMapLocationClientOption();
            //设置定位回调监听
            mLocationClient.setLocationListener(this);
            //设置为高精度定位模式
            mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
            //设置定位参数
            mLocationClient.setLocationOption(mLocationOption);
            mLocationClient.startLocation();//启动定位
        }
    }

    @Override
    public void deactivate() {
        mListener = null;
        if (mLocationClient != null) {
            mLocationClient.stopLocation();
            mLocationClient.onDestroy();
        }
        mLocationClient = null;
    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        mAMapLocation = aMapLocation;
        if (mListener != null && aMapLocation != null) {
            if (aMapLocation != null
                    && aMapLocation.getErrorCode() == 0) {
                mListener.onLocationChanged(aMapLocation);// 显示系统小蓝点
            } else {
                String errText = "定位失败," + aMapLocation.getErrorCode() + ": " + aMapLocation.getErrorInfo();
                Logger.e("AmapErr" + errText);
            }
        }
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        Intent intent = new Intent(this, ChangeOrderActivity.class);
        intent.putExtra("order_info", mOrderVO);
        startActivity(intent);
        return false;

    }


    /**
     * 监听取消
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventBus(EventEntity obj) {
        switch (obj.what) {
            case ORDER_CANCLE:
                mPresenter.selectDriverOrderDetails(mOrderVO.getOrderNo(), mOrderVO.getUseCarType());
                break;

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMapOrder.onDestroy();
        RxBus.getDefault().unregister(this);

        if (null != mLocationClient) {
            mLocationClient.onDestroy();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mMapOrder.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mMapOrder.onPause();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mMapOrder.onSaveInstanceState(outState);
    }

    @Override
    public void showEndTaskSuccess(EndPushVO endPushVO) {

    }


    @Override
    public void showEndTaskError(String errorMsg) {

    }

    @Override
    public void showStartTaskSuccess() {

    }

    @Override
    public void showStartTaskError(String errorMsg) {
        showMsg(errorMsg);
    }

    @Override
    public void showOrderDetailSuccess(OrderVO data) {
        mStateView.showContent();
        mOrderVO = data;
        if (data.getDriverOrder() != null) {
            if (data.getDriverOrder().getDriverOrderStatus() == 2 && data.getOrderStatus() == 3) {
                mTvOrder.setText("到达乘客上车位置");
                mBtnOrderService.setText("导航至起点");
                initToolBar(mToolbar, "未开始", true, true);
            } else if (data.getDriverOrder().getDriverOrderStatus() == 1 && data.getOrderStatus() == 2) {
                mTvOrder.setText("立即前往");
                mBtnOrderService.setText("导航至起点");
                initToolBar(mToolbar, "未开始", true, true);
            } else if (data.getDriverOrder().getDriverOrderStatus() == 3 && data.getOrderStatus() == 3) {
                mTvOrder.setText("乘客已上车");
                initToolBar(mToolbar, "等待乘客上车", true, true);
                mBtnOrderService.setText("联系客服");
            } else if (data.getDriverOrder().getDriverOrderStatus() == 0 || data.getOrderStatus() == 0) {
                initToolBar(mToolbar, "", true, true);
                mTvOrder.setVisibility(View.GONE);
                mBtnOrderService.setVisibility(View.GONE);
                CancleOrderDialog dialog = new CancleOrderDialog(OrderInfoActivity.this, mOrderVO, false);
                dialog.show();
                dialog.setListener(this);
            } else if (data.getDriverOrder().getDriverOrderStatus() == 5) {
//                initToolBar(mToolbar, "已完成", true, true);
            }
        }
        mTvOrderStartAddress.setText(data.getDeparture());
        //设置发单用户信息
        if (data.getUser() != null) {
            AM.image().bindToCircleObject(data.getUser().getAvatarUrl(), mIvOrderAvatar);
            mTvOrderUsername.setText(data.getUser().getUsername());
            mIvOrderGender.setImageResource(TextUtils.isEmpty(data.getUser().getSex()) ? R.mipmap.ic_gender_man : data.getUser().getSex().equals(getString(R.string.text_user_man)) ? R.mipmap.ic_gender_man : R.mipmap.ic_gender_women);
        }

        mTvOrderEndAddress.setText(data.getDestination());
        mTvOrderTime.setText(String.format("全程约%skm", data.getDistance()));
        mTvOrderRemark.setText(TextUtils.isEmpty(data.getMessages()) ? "暂无备注" : data.getMessages());
        mTvOrderLabel.setVisibility(TextUtils.isEmpty(data.getServiceDesc()) ? View.GONE : View.VISIBLE);
        mTvOrderLabel.setText(data.getServiceDesc().replaceAll(",", "  "));
        setfromandtoMarker(data);

        mTvOrderPoint1.setVisibility(TextUtils.isEmpty(data.getWaypoint1()) ? View.GONE : View.VISIBLE);
        mTvOrderPoint2.setVisibility(TextUtils.isEmpty(data.getWaypoint2()) ? View.GONE : View.VISIBLE);
        mTvOrderPoint1.setText(TextUtils.isEmpty(data.getWaypoint1()) ? "" : "途径点：" + data.getWaypoint1());
        mTvOrderPoint2.setText(TextUtils.isEmpty(data.getWaypoint2()) ? "" : "途径点：" + data.getWaypoint2());
//        if (data.getUseCarType() == 1) {//接机单
//            mTvOrderArrivedTime.setText(DateUtil.foramtArrivedTime(data.getReceiveTime(), data.getDuration()));
//        } else if (data.getUseCarType() == 0) {//用车
//            if (data.getOrderType() == 0) {//现在
//                mTvOrderArrivedTime.setText(DateUtil.foramtArrivedTime(data.getCreateTime(), data.getDuration()));
//            } else if (data.getOrderType() == 1) {//预约
//                mTvOrderArrivedTime.setText(DateUtil.foramtArrivedTime(data.getReceiveTime(), data.getDuration()));
//
//            } else if (data.getOrderType() == 2) {//代叫
//                if (data.getOrderFlag() == 0) {//现在出发
//                    mTvOrderArrivedTime.setText(DateUtil.foramtArrivedTime(data.getCreateTime(), data.getDuration()));
//                } else if (data.getOrderFlag() == 1) {//预约时间
//                    mTvOrderArrivedTime.setText(DateUtil.foramtArrivedTime(data.getReceiveTime(), data.getDuration()));
//                }
//            }
//
//        }
    }

    @Override
    public void showOrderDetailError(String errorMsg) {
        mStateView.showRetry();
    }

    @Override
    public void showArriveAtLocationSuccess() {
        mPresenter.selectDriverOrderDetails(mOrderVO.getOrderNo(), mOrderVO.getUseCarType());
    }

    @Override
    public void showPassengersGettingOnBusSuccess() {
        Intent intent = new Intent(this, EndTaskActivity.class);
        intent.putExtra("order_info", mOrderVO);
        startActivity(intent);
        finish();
    }

    @Override
    public void showGoNowSuccess() {
        mPresenter.selectDriverOrderDetails(mOrderVO.getOrderNo(), mOrderVO.getUseCarType());
    }

    @Override
    public void showComfirmTripSuccess() {

    }


    /**
     * 在线沟通
     */
    @OnClick(R.id.btn_order_triping_chat)
    public void onViewChatClicked() {
        try {
            RongIM.getInstance().startPrivateChat(this,
                    "24",
                    "https://upload.jianshu.io/users/upload_avatars/15127138/da8d9d4e-dd7c-438c-b3d1-e27d02a2166c?imageMogr2/auto-orient/strip|imageView2/1/w/240/h/240");
        } catch (Exception e) {
            T.showShort(e.getMessage());
        }
    }

    /**
     * 打电话
     */

    @OnClick(R.id.btn_order_triping_phone)
    public void onViewPhoneClicked() {
        if (mOrderVO != null && mOrderVO.getUser() != null) {
            initDialog(mOrderVO.getUser().getMobile());
        }
    }

    @Override
    public void onMapClick(LatLng latLng) {

    }

    @Override
    public boolean onMarkerClick(Marker marker) {
        return false;
    }

    /**
     * info
     */
    @Override
    public void onInfoWindowClick(Marker marker) {

    }

    @Override
    public View getInfoWindow(Marker marker) {
        View infoWindow = getLayoutInflater().inflate(
                R.layout.window_map, null);
        render(marker, infoWindow);
        return infoWindow;
    }

    /**
     * 自定义infowinfow窗口
     */
    public void render(Marker marker, View view) {
        TextView imageView = view.findViewById(R.id.tv_map_window_km);
        imageView.setText(String.format("全程约%skm", mOrderVO.getDistance()));
    }

    @Override
    public View getInfoContents(Marker marker) {
        return null;
    }

    @Override
    public void onBusRouteSearched(BusRouteResult busRouteResult, int i) {

    }

    @Override
    public void onDriveRouteSearched(DriveRouteResult result, int errorCode) {
        aMap.clear();// 清理地图上的所有覆盖物
        mDriveRouteResult = result;
        if (errorCode == AMapException.CODE_AMAP_SUCCESS) {
            if (result != null && result.getPaths() != null) {
                if (result.getPaths().size() > 0) {
                    try {
                        DrivePath walkPath = mDriveRouteResult.getPaths()
                                .get(0);
                        DriveRouteOverlay driveRouteOverlay = new DriveRouteOverlay(
                                this, aMap, walkPath,
                                mDriveRouteResult.getStartPos(),
                                mDriveRouteResult.getTargetPos(),
                                mDriveRouteResult.getDriveQuery().getPassedByPoints(), false
                        );

                        driveRouteOverlay.getWalkColor();//轨迹颜色修改
                        driveRouteOverlay.removeFromMap();
                        driveRouteOverlay.addToMap();
                        driveRouteOverlay.zoomToSpan();
                        driveRouteOverlay.setNodeIconVisibility(false);//关闭行走图标轨迹
                        int dis = (int) walkPath.getDistance();
                        int dur = (int) walkPath.getDuration();
                        String des = AMapUtil.getFriendlyTime(dur) + "(" + AMapUtil.getFriendlyLength(dis) + ")";
                    } catch (Exception e) {
                        Logger.e("onDriveRouteSearched" + e.getMessage());
                    }

                } else if (result != null && result.getPaths() == null) {
                    showMsg(R.string.toast_no_result);
                }
            } else {
                showMsg(R.string.toast_no_result);
            }
        } else {
            switch (errorCode) {
                case 1800:
                    showMsg("服务端新增错误,无法规划路线");
                    break;
                case 1802:
                case 1804:
                case 1806:
                    showMsg("请先检查网络状况是否良好");
                    break;
                case 1901:
                    showMsg("请检查参数设定是否正确");
                    break;
            }

        }
    }

    @Override
    public void onWalkRouteSearched(WalkRouteResult result, int errorCode) {

    }

    @Override
    public void onRideRouteSearched(RideRouteResult rideRouteResult, int i) {

    }

    /**
     * 乘客相关操作
     */
    @OnClick(R.id.btn_order_passengers)
    public void onViewClicked() {
        if (mOrderVO != null && mOrderVO.getDriverOrder() != null) {
            if (mOrderVO.getDriverOrder().getDriverOrderStatus() == 2 && mOrderVO.getOrderStatus() == 3) {
                mPresenter.arriveAtLocation(mOrderVO.getOrderNo(), mOrderVO.getUseCarType());
            } else if (mOrderVO.getDriverOrder().getDriverOrderStatus() == 1 && mOrderVO.getOrderStatus() == 2) {
                mPresenter.goNow(mOrderVO.getOrderNo(), mOrderVO.getUseCarType());
            } else if (mOrderVO.getDriverOrder().getDriverOrderStatus() == 3 && mOrderVO.getOrderStatus() == 3) {
                mPresenter.passengersGettingOnBus(mOrderVO.getOrderNo(), mOrderVO.getUseCarType());
            }
        }
    }


    private void initDialog(String msg) {
        mDialog = new NormalDialog(this);
        mDialog.content(msg);
        mDialog.title("是否拨打？");
        mDialog.btnNum(2);
        mDialog.setOnBtnClickL(new OnBtnClickL() {
            @Override
            public void onBtnClick() {
                mDialog.dismiss();
            }
        }, new OnBtnClickL() {
            @SuppressLint("CheckResult")
            @Override
            public void onBtnClick() {
                mDialog.dismiss();
                rxPermissions
                        .request(Manifest.permission.CALL_PHONE)
                        .subscribe(granted -> {
                            if (granted) { // Always true pre-M
                                diallPhone(msg);
                            } else {
                                showMsg("请先开启拨打电话权限!");
                            }
                        });
            }
        });
        mDialog.show();
    }

    /**
     * 拨打电话（跳转到拨号界面，用户手动点击拨打）
     *
     * @param phoneNum 电话号码
     */
    public void diallPhone(String phoneNum) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_CALL);
        //url:统一资源定位符
        //uri:统一资源标示符（更广）
        intent.setData(Uri.parse("tel:" + phoneNum));
        //开启系统拨号器
        startActivity(intent);
    }

    @OnClick(R.id.btn_order_service)
    public void onViewServiveClicked() {
        if (mOrderVO == null) return;
        if (mAMapLocation == null) return;
        if (mOrderVO.getDriverOrder().getDriverOrderStatus() == 2 || mOrderVO.getDriverOrder().getDriverOrderStatus() == 1) {
            if (!LocationUtils.isGpsEnabled()) {
                initGpsDialog();
            } else {
                if (mAMapLocation != null) {
//                    mDriveRouteResult.getDriveQuery().getPassedByPoints();
                    Poi start = new Poi(mAMapLocation.getAddress(), new LatLng(mAMapLocation.getLatitude(), mAMapLocation.getLongitude()), "");
                    Poi end = new Poi(mOrderVO.getDeparture(), new LatLng(mOrderVO.getDepLatitude(), mOrderVO.getDepLongitude()), "");
                    AmapNaviPage.getInstance().showRouteActivity(this, new AmapNaviParams(start, null, end, AmapNaviType.DRIVER), null);
                } else {
                    initMapLocation();
                }
            }

        } else {
            showMsg("此功能正在开发中...");
        }
    }


    private void initGpsDialog() {
        NormalDialog dialog = new NormalDialog(this);
        dialog.content("导航需要GPS，请先打开GPS");
        dialog.isTitleShow(false);
        dialog.btnNum(2);
        dialog.setOnBtnClickL(new OnBtnClickL() {
            @Override
            public void onBtnClick() {
                dialog.dismiss();
            }
        }, new OnBtnClickL() {
            @Override
            public void onBtnClick() {
                dialog.dismiss();
                LocationUtils.openGpsSettings();
            }
        });
        dialog.show();
    }


    //取消订单弹框关闭
    @Override
    public void doConfirm() {
        finish();
    }


}
